/**
 * 是否国内手机号码
 * @param val
 * @returns {boolean}
 */
export function isPhone(val: string) {
  return /^1[3456789]\d{9}$/.test(val)
}

/**
 * 是否座机号码
 * @param val
 * @returns {boolean}
 */
export function isLandline(val: string) {
  return /^\d{3,4}-?\d{6,8}$/.test(val)
}

/**
 * 是否资源url地址
 * @param val
 * @returns {boolean}
 */
export function isUrl(val: string) {
  return /^(https?|wss?|ftp|file):\/\/[-A-Za-z\d+&@#/%?=~_|!:,.;]+[-A-Za-z\d+&@#/%=~_|]$/.test(
    val
  )
}

/**
 * 是否邮箱地址
 * @param val
 * @returns {boolean}
 */
export function isEmail(val: string) {
  return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(val)
}

/**
 * 是否ip地址
 * @param val
 * @returns {boolean}
 */
export function isIp(val: string) {
  return /^((2((5[0-5])|([0-4]\d)))|([0-1]?\d{1,2}))(\.((2((5[0-5])|([0-4]\d)))|([0-1]?\d{1,2}))){3}$/.test(
    val
  )
}

/**
 * 是否身份证号码格式
 * @param val
 * @param useRe
 * @returns {boolean}
 */
export function isIdCard(val: string, useRe = false) {
  if (val.length < 18) return false
  if (useRe) {
    return /^[1-9]\d{5}(19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[\dXx]$/.test(
      val
    )
  }
  const weightedArr = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
  const codeArray = ['1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2']

  let sum = 0
  for (let i = 0; i < 17; i++) {
    sum += Number(val[i]) * weightedArr[i]
  }

  return codeArray[sum % 11] === val[17].toLowerCase()
}
